# -*- coding:utf-8 -*-

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


class Action():
    def __init__(self):
        # 初始化配置，设置Desired Capabilities参数
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "192.168.1.100:5555",
            "appPackage": "com.xingin.xhs",
            "appActivity": ".activity.SplashActivity",
            "udid": "192.168.1.100:5555",
            "noReset": "True"
        }
        # 指定Appium Server
        self.server = 'http://localhost:4723/wd/hub'
        # 新建一个Session
        self.driver = webdriver.Remote(self.server, self.desired_caps)
        self.driver.implicitly_wait(10)
        #获取屏幕的高
        self.x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        self.y = self.driver.get_window_size()['height']

    def comments(self):
        time.sleep(2)
        # app开启之后点击一次屏幕，确保页面的展示
        self.driver.tap([(300, 800)], 500)

    def scroll(self):
        # TouchAction(self.driver).tap(x=655, y=738).perform()
        # 无限滑动
        while True:
            TouchAction(self.driver).press(x=327, y=850).wait(300).move_to(x=349, y=578).release().perform()
            # time.sleep(5)
            # TouchAction(self.driver).press(x=467, y=957).wait(100).move_to(x=472, y=716).release().perform()

            # # 滑屏，大概从屏幕右边2分之一高度，往左侧滑动,滑动后显示的是 热点tab
            # self.driver.swipe(6/7*self.start_x, 1/2*self.start_y, 1/7*self.start_x, 1/2*self.start_y, 100)
            # #向右滑动，显示推荐tab 内容，第五个参数，时间设置大一点，否则容易看不到滑动效果
            # self.driver.swipe(1/7*self.start_x, 1/2*self.start_y, 5/7*self.start_x, 1/2*self.start_y, 200)
            #向上滑
            # self.driver.swipe(1 / 2 * self.x, 1 / 2 * self.y, 1 / 2 * self.x, 1 / 7 * self.y, 200)
            # # 向下滑动
            # self.driver.swipe(1/2*self.start_x, 1/7*self.start_y, 1/2*self.start_x, 6/7*self.start_y, 200)
            # 设置延时等待
            time.sleep(2)


    def send_content(self):
        time.sleep(3)
        TouchAction(self.driver).tap(x=336, y=153).perform()
        element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, "com.xingin.xhs:id/b3j")))
        element.send_keys('美迪惠尔面膜')

        # element1 = WebDriverWait(self.driver, 10).until(
        #     EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/css")))

        time.sleep(2)
        # 点击第一条
        TouchAction(self.driver).tap(x=234, y=179).perform()

        while True:
            userlist = WebDriverWait(self.driver, 10).until(
                EC.presence_of_all_elements_located((By.ID, "com.xingin.xhs:id/b3u")))
            for u in userlist:
                # t = u.get_attribute("bounds")
                # t= t.replace("[", "").split("]")
                # x1 = int(t[0].split(",")[0])
                # x2 = int(t[1].split(",")[0])
                # y1 = int(t[0].split(",")[1])
                # y2 = int(t[1].split(",")[1])
                # self.driver.swipe(x2 - 300, y2 + 100, x1 + 300, y1 + 50, 200)
                u.click()
                element = WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located((By.ID, "com.xingin.xhs:id/bco"))).click()
                # 循环拉取评论
                while True:
                    TouchAction(self.driver).press(x=353, y=1134).wait(200).move_to(x=353, y=900).release().perform()
                    try:
                        element1 = WebDriverWait(self.driver, 10).until(
                            EC.presence_of_element_located((By.ID, "com.xingin.xhs:id/ao_")))
                        if element1.text.endswith("条评论") or element1.text.endswith("更多评论"):
                            element1.click()
                        else:
                            break
                    except:
                        pass
                self.driver.keyevent(4) # 返回
            self.driver.swipe(353, 1120, 350, 300, 330)
            # com.xingin.xhs:id/bco 评论ID  com.xingin.xhs:id/ao_ 展开评论 com.xingin.xhs:id/a7g 搜索框
            try:
                locator = ('class', 'android.widget.TextView')
                WebDriverWait(self.driver, 1).until(
                    EC.text_to_be_present_in_element(locator, "暂时没有更多了"))
                # WebDriverWait(self.driver, 1).until(
                #     EC.text_to_be_present_in_element((By.XPATH, '//android.widget.TextView[contains(@text, "暂时没有更多了")]')))
            except:
                pass



    def pinglun(self):
        while True:
            time.sleep(3)
            # 点击评论按钮
            element = WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, '//android.widget.TextView[@resource-id="com.xingin.xhs:id/bco"]')))
            self.driver.tap([(353, 179)])
            element.click()
            # 循环拉取内容
            while True:
                # 滚动拉取
                # self.driver.swipe(353, 1134, 353, 337, 100)
                TouchAction(self.driver).press(x=353, y=1134).wait(100).move_to(x=353, y=337).release().perform()
                # 判断是否还有评论
                # try:
                #     locator = ('class', 'android.widget.TextView')
                #     WebDriverWait(self.driver, 1).until(
                #         EC.text_to_be_present_in_element(locator, "暂时没有更多了"))
                #     # WebDriverWait(self.driver, 1).until(
                #     #     EC.presence_of_element_located((By.XPATH, '//android.widget.TextView[contains(@text, "暂时没有更多了")]')))
                # except:
                #     pass

            self.driver.keyevent(4) # 返回
            # 换下个视频
            TouchAction(self.driver).press(x=327, y=850).wait(300).move_to(x=349, y=578).release().perform()


    def main(self):
        # self.comments()
        # self.scroll()
        self.send_content()
        # self.pinglun()



if __name__ == '__main__':
    action = Action()
    action.main()
